﻿$.widget("socaloud.matchDetailsWidget", {

    options: {
        template: 'templates/match-details-template.html',
        ticker: $.connection.matchTicker
    },

    _create: function () {
        var self = this;
        var ticker = self.options.ticker;

        var content = Extension.getTemplate(self.options.template);
        self.element.append(content);

        self.options.viewModel = {
            match: ko.observable({}),
            yellowState: function (player) {

                var match = self.options.viewModel.match();
                if (!!match && match.hasOwnProperty('HomeYellowCards') && match.hasOwnProperty('AwayYellowCards')) {

                    var res = [];
                    var yellows = match.HomeYellowCards.concat(match.AwayYellowCards);
                    for (var i in yellows) {
                        var yellow = yellows[i];
                        if (yellow.Player === player) {
                            res.push(yellow.Time);
                        }
                    }

                    return res.join(' ');
                }

                return '';
            },
            redState: function (player) {

                var match = self.options.viewModel.match();
                if (!!match && match.hasOwnProperty('HomeRedCards') && match.hasOwnProperty('AwayRedCards')) {

                    var reds = match.HomeRedCards.concat(match.AwayRedCards);
                    for (var j in reds) {
                        var red = reds[j];
                        if (red.Player === player) {
                            return red.Time;
                        }
                    }
                }

                return '';
            },
            outState: function (player) {

                var match = self.options.viewModel.match();
                if (!!match && match.hasOwnProperty('HomeSubsOut') && match.hasOwnProperty('AwaySubsOut')) {

                    var outs = match.HomeSubsOut.concat(match.AwaySubsOut);
                    for (var k in outs) {
                        var out = outs[k];
                        if (out.Player === player) {
                            return out.Time;
                        }
                    }

                }

                return '';
            },
            goalState: function (player) {

                var match = self.options.viewModel.match();
                if (!!match && match.hasOwnProperty('HomeScorers') && match.hasOwnProperty('AwayScorers')) {

                    var res = [];
                    var goals = match.HomeScorers.concat(match.AwayScorers);
                    for (var l in goals) {
                        var goal = goals[l];
                        if (goal.Player.indexOf(player) != -1) {
                            var time = goal.Time;
                            if (goal.Player.indexOf('Own') != -1) {
                                time += ' own';
                            }
                            res.push(time);
                        }
                    }
                    return res.join(' ');
                }

                return '';
            }
        };


        ko.applyBindings(self.options.viewModel, self.element[0]);


        function init(matchId) {
            return ticker.server.getMatch(matchId).done(function (match) {
                if (!!match) {
                    self.options.viewModel.match(match || {});
                }
            });
        }

        $.extend(ticker.client, {
            updateMatch: function (match) {
                if (!!match) {
                    console.log(match);
                    self.options.viewModel.match(match || {});
                }
            }
        });

        $.connection.hub.start()
            .pipe(function () {
                init(self.options.id);
            })
            .done(function (state) {
                console.log('done');
            });



        $('#timeline', self.element).twitterQueryWidget({ query: hashtags.join(' OR') + ' -RT' });

        for (var i in hashtags) {
            var tag = hashtags[i];
            var div = $('<div/>');
            div.twitterAudienceWidget({ query: tag });
            $('#audience', self.element).append(div);
        }

    },

    _destroy: function () {
        $.connection.hub.stop();
        alert('terminated');
        this.element.html('');
    }
});